package com.btb.adapter.service;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.btb.adapter.constant.Constants;
import com.btb.adapter.mapper.TransferRecordMapper;
import com.btb.adapter.model.TaskConfig;
import com.btb.adapter.model.TransferRecord;
import com.btb.adapter.model.TransferRecordExample;
import com.btb.adapter.vo.request.PayRequest;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class TransferRecordService {
	@Autowired
	private TransferRecordMapper transferRecordMapper;

	/**
	 * 	推送结果不是成功的
	 *
	 * 	检查区块不是有错误标记的
	 *
	 *	 推送的次数小于重试次数
	 *
	 * @param taskConfig
	 * @return
	 */
	public List<TransferRecord> query(final TaskConfig taskConfig) {
		Page<TransferRecord> page = PageHelper.startPage(1, Constants.CONFIRM_PAGE_SIZE).doSelectPage(() -> {
			transferRecordMapper.queryNotify(taskConfig.getRetryTimes());
		});
		return page.getResult();
	}

	public int updateConfirmPay(final Long id, final PayRequest payRequest,Long notifyBlockNumber, String notityStatus, String response, String notifyResult) {
		final TransferRecord transferRecord = transferRecordMapper.selectByPrimaryKey(id);
		int nextNotifyNum = Optional.ofNullable(transferRecord.getNotifyNum()).orElse(0) + 1;
		final TransferRecord record = TransferRecord.builder()
				.id(id).notifyTime(new Date())
				.notityStatus(notityStatus)
				.notifyResponse(response)
				.notifyBlockNumber(notifyBlockNumber)
				.notifyParam(JSON.toJSONString(payRequest))
				.notifyRid(payRequest.getRequestId())
				.notifyResult(notifyResult)
				.notifyNum(nextNotifyNum)  // 每推一次,  就通知的次数就加1
				.build();
		return transferRecordMapper.updateByPrimaryKeySelective(record);
	}
	public int updateConfirmPay(final Long id, final String notifyCheckResult) {
		TransferRecord record = TransferRecord.builder().id(id).notifyCheckResult(notifyCheckResult).build();
		return transferRecordMapper.updateByPrimaryKeySelective(record);
	}

	public TransferRecord find(final String transactionHash) {
		TransferRecordExample example = new TransferRecordExample();
		example.createCriteria().andTransactionHashEqualTo(transactionHash);
		return transferRecordMapper.selectByExample(example).stream().findAny().orElse(null);
	}
	public PayRequest queryPayResult(final String transactionHash) {
		final TransferRecord record = find(transactionHash);
		if(Objects.isNull(record)) {
			return null;
		}
		PayRequest payRequest = PayRequest.builder()
				.requestId(record.getNotifyRid())
				.transactionHash(transactionHash)
				.address(record.getFromAddress())
				.status(record.isOk())
				.orderId(record.getPayOrderId())
				.type(record.getPayType())
				.orderPrice(record.getPayOrderPrice().stripTrailingZeros().toPlainString())
				.time(Long.parseLong(record.getTimestamp()))
				.build();
		return payRequest;
	}
}
